package com.lsus.golkhadi.server;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.lsus.golkhadi.client.ICardService;
import com.lsus.golkhadi.shared.Card;
import com.lsus.golkhadi.shared.Player;
import com.lsus.golkhadi.shared.Position;
import com.lsus.golkhadi.shared.Table;

public class CardServiceImpl extends RemoteServiceServlet implements ICardService{

	private List<Card> deck = new ArrayList<Card>(Arrays.asList(Card.CLUB1,Card.CLUB2,Card.CLUB3,Card.CLUB4,Card.CLUB5,Card.CLUB6,Card.CLUB7,Card.CLUB8,Card.CLUB9,Card.CLUB10,Card.CLUB11,Card.CLUB12,Card.CLUB13,
			Card.DIAMOND1,Card.DIAMOND2,Card.DIAMOND3,Card.DIAMOND4,Card.DIAMOND5,Card.DIAMOND6,Card.DIAMOND7,Card.DIAMOND8,Card.DIAMOND9,Card.DIAMOND10,Card.DIAMOND11,Card.DIAMOND12,Card.DIAMOND13,
			Card.SPADE1,Card.SPADE2,Card.SPADE3,Card.SPADE4,Card.SPADE5,Card.SPADE6,Card.SPADE7,Card.SPADE8,Card.SPADE9,Card.SPADE10,Card.SPADE11,Card.SPADE12,Card.SPADE13,
			Card.HEART1,Card.HEART2,Card.HEART3,Card.HEART4,Card.HEART5,Card.HEART6,Card.HEART7,Card.HEART8,Card.HEART9,Card.HEART10,Card.HEART11,Card.HEART12,Card.HEART13));
	private Random rgen;
	private Table table = new Table();
	
	public boolean suffle(String sessionId){
		if (LoginServiceImpl.verifyAuthorization(sessionId)){
			rgen = new Random();
			for(int i=0; i<deck.size(); i++){
				int randomPosition = rgen.nextInt(deck.size());
				Card temp = deck.get(i);
				deck.set(i, deck.get(randomPosition));
				deck.set(randomPosition, temp);
			}
		}
		return false;
	}
	
	public List<Card> getMyCards(String sessionId){
		if (LoginServiceImpl.verifyAuthorization(sessionId)){
			List<Card> playerCards = new ArrayList<Card>();
			String name = "";
			name = LoginServiceImpl.getUserName(sessionId);
			if (name.equals("ligaj")){
				for (int i=0; i<13; i++){
					playerCards.add(deck.get(i));
				}
			}
			else if (name.equals("suresh")){
				for (int i=13; i<26; i++){
					playerCards.add(deck.get(i));
				}
			}
			else if (name.equals("subhash")){
				for (int i=26; i<39; i++){
					playerCards.add(deck.get(i));
				}		
			}
			else if (name.equals("uttam")){
				for (int i=39; i<52; i++){
					playerCards.add(deck.get(i));
				}
			}
			return playerCards;
		}
		return null;
	}
	
	public Table getUpdatedTable(String sessionId){
		if(LoginServiceImpl.verifyAuthorization(sessionId)){
			return table;
		}
		return null;
	}
	
	public boolean clearTable(String sessionId){
		if(LoginServiceImpl.verifyAuthorization(sessionId)){
			table = new Table();
			return true;
		}
		return false;
	}
	
	public boolean updateTableWithMyCard(Player player, Card myCard){
		if (LoginServiceImpl.verifyAuthorization(player.getSessionId())){
			if (player.getPosition()==Position.PLAYER1){
				table.setPlayer1Card(myCard);
				return true;
			}
			if (player.getPosition()==Position.PLAYER2){
				table.setPlayer2Card(myCard);		
				return true;
			}
			if (player.getPosition()==Position.PLAYER3){
				table.setPlayer3Card(myCard);
				return true;
			}
			if (player.getPosition()==Position.PLAYER4){
				table.setPlayer4Card(myCard);
				return true;
			}
		}
		return false;
	}
}
